﻿using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;

public class ConfigDraw : MonoBehaviour
{
    [SerializeField] RectTransform workingArea;
    [SerializeField] CustomUvMapper config;
    RawImage ri;
    Texture2D t2d;
    Color[] empty;
    // Start is called before the first frame update
    void Start()
    {
        ri = gameObject.AddComponent<RawImage>();
        ri.raycastTarget = false;
        CheckDataSize();
    }

    private void Update()
    {
        CheckDataSize();
        Draw();
    }

    private void CheckDataSize()
    {
        if (t2d == null || empty == null
            || t2d.width != Screen.width
            || t2d.height != Screen.height)
        {
            t2d = new Texture2D(Screen.width, Screen.height);
            t2d.name = "texture " + t2d.width + " x " + t2d.height;
            empty = new Color[t2d.width * t2d.height];
            ri.texture = t2d;

            //Draw();
        }
    }


    Color drawingColor;
    AreaRect area;
    /// <summary>
    /// 根据配置参数，画出主要的五条经线和七条纬线（宽度三像素）
    /// </summary>
    public void Draw()
    {
        area = new AreaRect(workingArea);

        t2d.SetPixels(empty);

        drawingColor = new Color(0.2f, 0.2f, 0.2f, 0.8f);
        var factor = 1 / 6f;

        DrawLongitude(0 * factor);
        DrawLongitude(1 * factor);
        DrawLongitude(2 * factor);
        DrawLongitude(3 * factor);
        DrawLongitude(4 * factor);
        DrawLongitude(5 * factor);
        DrawLongitude(6 * factor);

        factor = 1 / 180f;

        DrawLatitude(0.5f - (66 + 34f / 60)  * factor);
        DrawLatitude(0.5f - 60               * factor);
        DrawLatitude(0.5f - 30               * factor);
        DrawLatitude(0.5f - (23 + 26f / 60)  * factor);
        DrawLatitude(0.5f);
        DrawLatitude(0.5f + (23 + 26f / 60)  * factor);
        DrawLatitude(0.5f + 30               * factor);
        DrawLatitude(0.5f + 60               * factor);
        DrawLatitude(0.5f + (66 + 34f / 60)  * factor);

        t2d.Apply();

    }

    private void DrawLongitude(float pos01)
    {
        var centerX = area.GetXBy01(pos01);
        for (int y = area.minY; y <= area.maxY; y++)
        {
            var origin = area.Get01(centerX, y);
            var target = config.ToTarget(origin);
            if (target.x < 0 || target.x > 1 || target.y < 0 || target.y > 1)
                continue;
            var targetInt = area.GetBy01(target);
            if (targetInt.x < 0 || targetInt.x >= t2d.width
                || targetInt.y < 0 || targetInt.y >= t2d.height)
                continue;

            t2d.SetPixel(targetInt.x, targetInt.y, drawingColor);
            if (targetInt.x + 1 < Screen.width)
                t2d.SetPixel(targetInt.x + 1, targetInt.y, drawingColor);
            if (targetInt.x - 1 > 0)
                t2d.SetPixel(targetInt.x - 1, targetInt.y, drawingColor);
        }

    }

    private void DrawLatitude(float pos01)
    {
        var centerY = area.GetYBy01(pos01);

        for (int x = area.minX; x <= area.maxX; x++)
        {
            var origin = area.Get01(x, centerY);
            var target = config.ToTarget(origin);
            if (target.x < 0 || target.x > 1 || target.y < 0 || target.y > 1)
                continue;
            var targetInt = area.GetBy01(target);
            if (targetInt.x < 0 || targetInt.x >= t2d.width
                || targetInt.y < 0 || targetInt.y >= t2d.height)
                continue;


            t2d.SetPixel(targetInt.x, targetInt.y, drawingColor);
            if (targetInt.y + 1 < Screen.height)
                t2d.SetPixel(targetInt.x, targetInt.y + 1, drawingColor);
            if (targetInt.y - 1 > 0)
                t2d.SetPixel(targetInt.x, targetInt.y - 1, drawingColor);
        }

    }



}
